//
// Copyright (c) 1996 Molecular Structure Corporation
//
// RESTRICTED RIGHTS NOTICE SHORT FORM (JUNE 1987)
//
// Use, reproduction, or disclosure is subject to restrictions set
// forth in Contract No. W-31-109-ENG-38 and Contract No. 
// 943072401 with the University of Chicago, Operator of
// Argonne National Laboratory.
//
// CUIeditdetector.cc       Initial author: J.W. Pflugrath           20-Jan-1997
//    This file implements interface class CUIeditdetector.
/*
 *
 * Copyright (C) 2014 Rigaku Americas Corporation
 *                    9009 New Trails Drive
 *                    The Woodlands, TX, USA  77381
 *
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 *    * Redistributions of source code must retain the above copyright
 *      notice(s), this list of conditions and the following disclaimer.
 *    * Redistributions in binary form must reproduce the above copyright
 *      notice(s), this list of conditions and the following disclaimer in the
 *      documentation and/or other materials provided with the distribution.
 *    * Neither the name of the Rigaku Americas Corporation nor the 
 *      names of its contributors may be used to endorse or promote products
 *      derived from this software without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 * ARE DISCLAIMED. IN NO EVENT SHALL RIGAKU AMERICAS CORPORATION BE LIABLE 
 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
 * SERVICES; LOSS OF USE, DATA OR PROFITS; OR BUSINESS INTERUPTION) HOWEVER
 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
 * DAMAGE.
 *
*/ 

//
// README: Portions of this file are merged at file generation
// time. Edits can be made *only* in between specified code blocks, look
// for keywords <Begin user code> and <End user code>.
//
/////////////////////////////////////////////////////////////
//
// Source file for CUIeditdetector
//
//    This file generated by Builder Xcessory 3.5.1.
//
//    This class is a user interface "component", as described
//    in "Object-Oriented Programming with C++ and OSF/Motif",
//    by Douglas Young, Prentice Hall, 1992. ISBN 0-13-630252-1
//
/////////////////////////////////////////////////////////////
#include <X11/StringDefs.h>
#include <Xm/SelectioB.h>
#include <Xm/Form.h>
#include <Xm/Label.h>
#include "CUIeditdetector.h"
#include "CUIom2.h"

//
// Convenience functions from utilities file.
//
extern void RegisterBxConverters(XtAppContext);
extern XtPointer CONVERT(Widget, char *, char *, int, Boolean *);
extern XtPointer DOUBLE(double);
extern XtPointer SINGLE(float);
extern void MENU_POST(Widget, XtPointer, XEvent *, Boolean *);
extern Pixmap XPM_PIXMAP(Widget, char**);
extern void SET_BACKGROUND_COLOR(Widget, ArgList, Cardinal*, Pixel);


// Begin user code block <head> 

Cstring CUIeditdetector::ms_sBinMode      = "CCD_DETECTOR_BIN_MODE";
Cstring CUIeditdetector::ms_sReadMode     = "CCD_DETECTOR_READ_MODE";
Cstring CUIeditdetector::ms_sDezingerMode = "CCD_DETECTOR_DEZINGER_MODE";

// End user code block <head> 

//
// This table is used to define class resources that are placed
// in app-defaults. This table is necessary so each instance
// of this class has the proper default resource values set.
// This eliminates the need for each instance to have
// its own app-defaults values. This table must be NULL terminated.
//
#if defined(cxx_bx_compat)

Boolean CUIeditdetector::_initAppDefaults = True;
UIAppDefault  CUIeditdetector::_appDefaults[] = {
    {"", NULL, "dialogTitle", NULL},
    {"lbDezingerMode", NULL, "labelString", NULL},
    {"lbReadMode", NULL, "labelString", NULL},
    {"lbBinMode", NULL, "labelString", NULL},
    {"cUIomDezingerMode", "pb1", "labelString", NULL},
    {"cUIomDezingerMode", "pb0", "labelString", NULL},
    {"cUIomReadMode", "pb1", "labelString", NULL},
    {"cUIomReadMode", "pb0", "labelString", NULL},
    {"cUIomBinMode", "pb1", "labelString", NULL},
    {"cUIomBinMode", "pb0", "labelString", NULL},
    {NULL, NULL, NULL, NULL}
};
#endif

// These are default resources for widgets in objects of this class
// All resources will be prepended by *<name> at instantiation,
// where <name> is the name of the specific instance, as well as the
// name of the baseWidget. These are only defaults, and may be overriden
// in a resource file by providing a more specific resource name

String  CUIeditdetector::_defaultCUIeditdetectorResources[] = {
        NULL
};

//
// Old style constructor, for compatibility only.
//
CUIeditdetector::CUIeditdetector(const char *name, Widget parent) : 
    UIComponent(name)
{
    
    // Begin user code block <alt_constructor> 
    // End user code block <alt_constructor> 
    create(parent);
}

//
// Class constructor.
//
CUIeditdetector::CUIeditdetector(const char *name) : UIComponent(name)
{
    
    // Begin user code block <constructor> 
  m_prvCallback = NULL;
    // End user code block <constructor> 
}

//
// Minimal Destructor. Base class destroys widgets.
//
CUIeditdetector::~CUIeditdetector() 
{
    
    // Begin user code block <destructor> 
  if (NULL != m_poXhelp)
    {
      delete m_poXhelp;
      m_poXhelp = NULL;
    }
    // End user code block <destructor> 
    delete _clientDataStructs;
}

//
// Handle creation of all widgets in the class.
//
void CUIeditdetector::create(Widget parent) 
{
    Arg      args[256];
    Cardinal ac=0;
    Boolean  argok=False;
    
    //
    // Load any class-defaulted resources for this object.
    //
    setDefaultResources(parent, _defaultCUIeditdetectorResources);
    
#if defined(cxx_bx_compat)
    //
    // Setup app-defaults fallback table if not already done.
    //
    if (_initAppDefaults)
    {
        initAppDefaults(parent, "CUIeditdetector", _appDefaults);
        _initAppDefaults = False;
    }
    //
    // Now set the app-defaults for this instance.
    //
    setAppDefaults(parent, _appDefaults);
#endif
    
    //
    // Register the converters for the widgets.
    //
    RegisterBxConverters(XtWidgetToApplicationContext(parent)); 
    XtInitializeWidgetClass(xmSelectionBoxWidgetClass);
    XtInitializeWidgetClass(xmFormWidgetClass);
    XtInitializeWidgetClass(xmLabelWidgetClass);
    
    //
    // Create an unmanaged widget as the top of the hierarchy.
    //
    
    ac = 0;
    XtSetArg(args[ac], XmNdialogType, XmDIALOG_SELECTION); ac++;
#if (XmVersion >= 1002)
    XtSetArg(args[ac], XmNchildPlacement, XmPLACE_TOP); ac++;
#endif
    XtSetArg(args[ac], XmNx, 116); ac++;
    XtSetArg(args[ac], XmNy, 220); ac++;
    XtSetArg(args[ac], XmNwidth, 452); ac++;
    XtSetArg(args[ac], XmNheight, 545); ac++;
    _CUIeditdetector = XmCreateSelectionBox(parent,
        _name,
        args, 
        ac);
    _clientDataStructs = new UICallbackStruct;
    _clientDataStructs->object = this;
    _clientDataStructs->client_data = (XtPointer)0;
    XtAddCallback(_CUIeditdetector,
        XmNhelpCallback,
        CUIeditdetector::vEditDetectorCBCallback,
        (XtPointer)_clientDataStructs);
    _clientDataStructs = new UICallbackStruct;
    _clientDataStructs->object = this;
    _clientDataStructs->client_data = (XtPointer)0;
    XtAddCallback(_CUIeditdetector,
        XmNokCallback,
        CUIeditdetector::vEditDetectorCBCallback,
        (XtPointer)_clientDataStructs);
    _clientDataStructs = new UICallbackStruct;
    _clientDataStructs->object = this;
    _clientDataStructs->client_data = (XtPointer)0;
    XtAddCallback(_CUIeditdetector,
        XmNapplyCallback,
        CUIeditdetector::vEditDetectorCBCallback,
        (XtPointer)_clientDataStructs);
    _clientDataStructs = new UICallbackStruct;
    _clientDataStructs->object = this;
    _clientDataStructs->client_data = (XtPointer)0;
    XtAddCallback(_CUIeditdetector,
        XmNcancelCallback,
        CUIeditdetector::vEditDetectorCBCallback,
        (XtPointer)_clientDataStructs);
    
    _w = _CUIeditdetector;
    //
    // Install callback to guard against unexpected widget destruction..
    //
    installDestroyHandler();
    
    ac = 0;
    XtSetArg(args[ac], XmNresizePolicy, XmRESIZE_GROW); ac++;
    XtSetArg(args[ac], XmNx, 11); ac++;
    XtSetArg(args[ac], XmNy, 11); ac++;
    XtSetArg(args[ac], XmNwidth, 430); ac++;
    XtSetArg(args[ac], XmNheight, 205); ac++;
    _foEditDetector = XmCreateForm(_CUIeditdetector,
        "foEditDetector",
        args, 
        ac);
    XtManageChild(_foEditDetector);
    
    ac = 0;
    XtSetArg(args[ac], XmNtraversalOn, False); ac++;
    XtSetArg(args[ac], XmNx, 0); ac++;
    XtSetArg(args[ac], XmNy, 155); ac++;
    XtSetArg(args[ac], XmNwidth, 200); ac++;
    XtSetArg(args[ac], XmNheight, 40); ac++;
    _lbDezingerMode = XmCreateLabel(_foEditDetector,
        "lbDezingerMode",
        args, 
        ac);
    XtManageChild(_lbDezingerMode);
    
    ac = 0;
    XtSetArg(args[ac], XmNtraversalOn, False); ac++;
    XtSetArg(args[ac], XmNx, 0); ac++;
    XtSetArg(args[ac], XmNy, 90); ac++;
    XtSetArg(args[ac], XmNwidth, 200); ac++;
    XtSetArg(args[ac], XmNheight, 40); ac++;
    _lbReadMode = XmCreateLabel(_foEditDetector,
        "lbReadMode",
        args, 
        ac);
    XtManageChild(_lbReadMode);
    
    ac = 0;
    XtSetArg(args[ac], XmNtraversalOn, False); ac++;
    XtSetArg(args[ac], XmNx, 0); ac++;
    XtSetArg(args[ac], XmNy, 25); ac++;
    XtSetArg(args[ac], XmNwidth, 200); ac++;
    XtSetArg(args[ac], XmNheight, 40); ac++;
    _lbBinMode = XmCreateLabel(_foEditDetector,
        "lbBinMode",
        args, 
        ac);
    XtManageChild(_lbBinMode);
    
    #if defined(cxx_bx_compat)
    setAppDefaults(_foEditDetector, _appDefaults, "cUIomDezingerMode");
    #endif
    _cUIomDezingerMode = new CUIom2("cUIomDezingerMode");
    _cUIomDezingerMode->create(_foEditDetector);
    _cUIomDezingerMode->manage();
    ac = 0;
    XtSetArg(args[ac], XmNx, 205); ac++;
    XtSetArg(args[ac], XmNy, 155); ac++;
    XtSetArg(args[ac], XmNwidth, 147); ac++;
    XtSetArg(args[ac], XmNheight, 37); ac++;
    XtSetValues(_cUIomDezingerMode->baseWidget(), args, ac);
    
    #if defined(cxx_bx_compat)
    setAppDefaults(_foEditDetector, _appDefaults, "cUIomReadMode");
    #endif
    _cUIomReadMode = new CUIom2("cUIomReadMode");
    _cUIomReadMode->create(_foEditDetector);
    _cUIomReadMode->manage();
    ac = 0;
    XtSetArg(args[ac], XmNx, 205); ac++;
    XtSetArg(args[ac], XmNy, 90); ac++;
    XtSetArg(args[ac], XmNwidth, 147); ac++;
    XtSetArg(args[ac], XmNheight, 37); ac++;
    XtSetValues(_cUIomReadMode->baseWidget(), args, ac);
    
    #if defined(cxx_bx_compat)
    setAppDefaults(_foEditDetector, _appDefaults, "cUIomBinMode");
    #endif
    _cUIomBinMode = new CUIom2("cUIomBinMode");
    _cUIomBinMode->create(_foEditDetector);
    _cUIomBinMode->manage();
    ac = 0;
    XtSetArg(args[ac], XmNx, 205); ac++;
    XtSetArg(args[ac], XmNy, 25); ac++;
    XtSetArg(args[ac], XmNwidth, 147); ac++;
    XtSetArg(args[ac], XmNheight, 37); ac++;
    XtSetValues(_cUIomBinMode->baseWidget(), args, ac);
    
    ac = 0;
    XtSetArg(args[ac], XmNtopAttachment, XmATTACH_WIDGET); ac++;
    XtSetArg(args[ac], XmNbottomAttachment, XmATTACH_NONE); ac++;
    XtSetArg(args[ac], XmNleftAttachment, XmATTACH_FORM); ac++;
    XtSetArg(args[ac], XmNtopWidget, _lbReadMode); ac++;
    XtSetArg(args[ac], XmNtopOffset, 25); ac++;
    XtSetArg(args[ac], XmNbottomOffset, 0); ac++;
    XtSetArg(args[ac], XmNleftOffset, 0); ac++;
    XtSetValues(_lbDezingerMode, args, ac);
    
    ac = 0;
    XtSetArg(args[ac], XmNtopAttachment, XmATTACH_WIDGET); ac++;
    XtSetArg(args[ac], XmNbottomAttachment, XmATTACH_NONE); ac++;
    XtSetArg(args[ac], XmNleftAttachment, XmATTACH_FORM); ac++;
    XtSetArg(args[ac], XmNtopWidget, _lbBinMode); ac++;
    XtSetArg(args[ac], XmNtopOffset, 25); ac++;
    XtSetArg(args[ac], XmNbottomOffset, 0); ac++;
    XtSetArg(args[ac], XmNleftOffset, 0); ac++;
    XtSetValues(_lbReadMode, args, ac);
    
    ac = 0;
    XtSetArg(args[ac], XmNtopAttachment, XmATTACH_FORM); ac++;
    XtSetArg(args[ac], XmNleftAttachment, XmATTACH_FORM); ac++;
    XtSetArg(args[ac], XmNtopOffset, 25); ac++;
    XtSetArg(args[ac], XmNleftOffset, 0); ac++;
    XtSetValues(_lbBinMode, args, ac);
    
    ac = 0;
    XtSetArg(args[ac], XmNtopAttachment, XmATTACH_OPPOSITE_WIDGET); ac++;
    XtSetArg(args[ac], XmNbottomAttachment, XmATTACH_NONE); ac++;
    XtSetArg(args[ac], XmNleftAttachment, XmATTACH_WIDGET); ac++;
    XtSetArg(args[ac], XmNrightAttachment, XmATTACH_NONE); ac++;
    XtSetArg(args[ac], XmNtopWidget, _lbDezingerMode); ac++;
    XtSetArg(args[ac], XmNleftWidget, _lbDezingerMode); ac++;
    XtSetArg(args[ac], XmNtopOffset, 0); ac++;
    XtSetArg(args[ac], XmNbottomOffset, 0); ac++;
    XtSetArg(args[ac], XmNleftOffset, 5); ac++;
    XtSetArg(args[ac], XmNrightOffset, 0); ac++;
    XtSetArg(args[ac], XmNresizable, False); ac++;
    XtSetValues(_cUIomDezingerMode->baseWidget(), args, ac);
    
    ac = 0;
    XtSetArg(args[ac], XmNtopAttachment, XmATTACH_OPPOSITE_WIDGET); ac++;
    XtSetArg(args[ac], XmNbottomAttachment, XmATTACH_NONE); ac++;
    XtSetArg(args[ac], XmNleftAttachment, XmATTACH_WIDGET); ac++;
    XtSetArg(args[ac], XmNrightAttachment, XmATTACH_NONE); ac++;
    XtSetArg(args[ac], XmNtopWidget, _lbReadMode); ac++;
    XtSetArg(args[ac], XmNleftWidget, _lbReadMode); ac++;
    XtSetArg(args[ac], XmNtopOffset, 0); ac++;
    XtSetArg(args[ac], XmNbottomOffset, 0); ac++;
    XtSetArg(args[ac], XmNleftOffset, 5); ac++;
    XtSetArg(args[ac], XmNrightOffset, 0); ac++;
    XtSetArg(args[ac], XmNresizable, False); ac++;
    XtSetValues(_cUIomReadMode->baseWidget(), args, ac);
    
    ac = 0;
    XtSetArg(args[ac], XmNtopAttachment, XmATTACH_OPPOSITE_WIDGET); ac++;
    XtSetArg(args[ac], XmNbottomAttachment, XmATTACH_NONE); ac++;
    XtSetArg(args[ac], XmNleftAttachment, XmATTACH_WIDGET); ac++;
    XtSetArg(args[ac], XmNrightAttachment, XmATTACH_NONE); ac++;
    XtSetArg(args[ac], XmNtopWidget, _lbBinMode); ac++;
    XtSetArg(args[ac], XmNleftWidget, _lbBinMode); ac++;
    XtSetArg(args[ac], XmNtopOffset, 0); ac++;
    XtSetArg(args[ac], XmNbottomOffset, 0); ac++;
    XtSetArg(args[ac], XmNleftOffset, 5); ac++;
    XtSetArg(args[ac], XmNrightOffset, 0); ac++;
    XtSetArg(args[ac], XmNresizable, False); ac++;
    XtSetValues(_cUIomBinMode->baseWidget(), args, ac);
    
    // Begin user code block <endcreate> 
    // Create the help object and add the help callback

    m_poXhelp     = new CXhelp (_w);

    _clientDataStructs = new UICallbackStruct;
    _clientDataStructs->object = (UIComponent *)m_poXhelp;
    _clientDataStructs->client_data = (XtPointer) "EditDetector$$";
    XtAddCallback(_CUIeditdetector,
        XmNhelpCallback,
        CXhelp::vHTMLHelpCallback,
        (XtPointer)_clientDataStructs);
#if (XmVersion >= 1002)
    // Add help callback for textFields

#endif

    // Fix up _sdEditDetector display

    XtUnmanageChild(XmSelectionBoxGetChild(baseWidget(), XmDIALOG_TEXT));
    XtUnmanageChild(XmSelectionBoxGetChild(baseWidget(), 
					   XmDIALOG_SELECTION_LABEL));

    XtUnmanageChild(XmSelectionBoxGetChild(baseWidget(), 
					   XmDIALOG_LIST));
    XtUnmanageChild(XmSelectionBoxGetChild(baseWidget(), 
					   XmDIALOG_LIST_LABEL));

    XtManageChild(XmSelectionBoxGetChild(baseWidget(),
					 XmDIALOG_APPLY_BUTTON));

    XtVaSetValues(baseWidget(), XmNdefaultButton,
		  XmSelectionBoxGetChild(baseWidget(),
					 XmDIALOG_APPLY_BUTTON),
		  NULL);


    // End user code block <endcreate> 
}

//
// Classname access.
//
const char *const CUIeditdetector::className()
{
    return ("CUIeditdetector");
}

void CUIeditdetector::vEditDetectorCBCallback(Widget w, 
		XtPointer clientData, XtPointer callData)
{
    UICallbackStruct *data = (UICallbackStruct *) clientData;
    CUIeditdetector *obj = (CUIeditdetector *)data->object;
    obj->vEditDetectorCB(w, data->client_data, callData);
}

void CUIeditdetector::vEditDetectorCB (Widget w, XtPointer clientData, XtPointer callData)
{
    
    // Begin user code block <vEditDetectorCB> 
  if (w == NULL)
    {
    }
  else if (w == baseWidget())
    {
      // Callback was (supposedly) from _cuiEditDetector selection dialog

//      cout << "Edit detector CB!\n";

      XmSelectionBoxCallbackStruct *pCBS 
	= (XmSelectionBoxCallbackStruct *)callData;
      if ( (XmCR_OK == pCBS->reason) || (XmCR_APPLY == pCBS->reason) )
	{
	  if (NULL != m_prvCallback)
	    {
	      m_prvCallback(m_pObj);
	    }
	}
      else if (XmCR_CANCEL == pCBS->reason)
	{
	  // Do nothing ...
	}
      else if (XmCR_HELP == pCBS->reason)
	{
	  // Post html help dialog ...
	}
    }

    // End user code block <vEditDetectorCB> 
}

// Begin user code block <tail> 

void CUIeditdetector::vSetValues(Cimage_header& roHeader)
{
  Cstring sTemp;
  int     nStat;
  int     nMode;

  nMode = 0;
  nStat = roHeader.nGetValue(ms_sBinMode, &nMode);
  if ( (0 == nMode) || (1 == nMode) )
    {
     _cUIomBinMode->vSetValue(nMode); 
    }
  nStat = roHeader.nGetValue(ms_sReadMode, &nMode);
  if ( (0 == nMode) || (1 == nMode) )
    {
     _cUIomReadMode->vSetValue(nMode); 
    }
  nStat = roHeader.nGetValue(ms_sDezingerMode, &nMode);
  if ( (0 == nMode) || (1 == nMode) )
    {
     _cUIomDezingerMode->vSetValue(nMode); 
    }
}

void CUIeditdetector::vGetValues(Cimage_header *poHeader)
{
  Cstring sTemp;
  int     nStat;
  int     nMode;

  nMode = _cUIomBinMode->nGetValue();
  if ( (0 == nMode) || (1 == nMode) )
    {
      nStat = poHeader->nReplaceValue(ms_sBinMode, nMode);
    }
  nMode = _cUIomReadMode->nGetValue(); 
  if ( (0 == nMode) || (1 == nMode) )
    {
      nStat = poHeader->nReplaceValue(ms_sReadMode, nMode);
    }
  nMode = _cUIomDezingerMode->nGetValue(); 
  if ( (0 == nMode) || (1 == nMode) )
    {
      nStat = poHeader->nReplaceValue(ms_sDezingerMode, nMode);
    }
}

// End user code block <tail> 
